﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LogisticsModel;

namespace LogisticsDAL
{
  public  class TruckTeamDAL
    {
        #region 车队列表
        public static List<TruckTeam> GetTruckTeams(int tid,string tName)
        {
            StringBuilder builder = new StringBuilder("select * from TruckTeam");

            List<TruckTeam> teams = new List<TruckTeam>();
            List<SqlParameter> parameters = new List<SqlParameter>();
            List<string> andList = new List<string>();
            if (tid > 0)
            {
                andList.Add("TeamID=@teamId");
                parameters.Add(new SqlParameter("@teamId", tid));
            }
            if (!string.IsNullOrWhiteSpace(tName))
            {
                andList.Add("Leader=@leader");
                parameters.Add(new SqlParameter("@leader", tName));

            }
          
            if (andList.Count > 0)
            {
                //string str= string.Join("and", andList.ToArray());
                builder.Append($" where {string.Join(" and ", andList.ToArray())}");


            }
            SqlDataReader reader = SqlHelper.ExecuteReader(builder.ToString(),parameters.ToArray());
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    TruckTeam team = new TruckTeam();
                    team.TeamName = reader["TeamName"].ToString();
                    team.TeamID = Convert.ToInt32(reader["TeamID"]);
                    team.Leader = reader["Leader"].ToString();
                    team.Remark = reader["Remark"].ToString();
                    teams.Add(team);
                }
            }
            reader.Close();
            return teams;

        }
        #endregion

        #region 车队修改
        public static int EditTruckTeam(TruckTeam truckTeam)
        {
            string sql = "update TruckTeam set TeamName=@TeamName,Leader=@leader,Remark=@remark where TeamID=@teamid ";
            SqlParameter [] parameter = {
                new SqlParameter("@teamName",truckTeam.TeamName),
                new SqlParameter("@leader",truckTeam.Leader),
                new SqlParameter("@remark",truckTeam.Remark),
                new SqlParameter("@teamid",truckTeam.TeamID),

            };
            return SqlHelper.ExecuteNonQuery(sql,parameter);
               
        }
        #endregion


    }
}
